package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.mapper.*;
import com.sky.service.LoginService;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

@Service
public class WorkspaceServiceImpl implements WorkspaceService {

    @Autowired
    private WorkspaceMapper workspaceMapper;

    @Autowired
    private LoginMapper loginMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderDetailMapper orderDetailMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public BusinessDataVO businessData(LocalDateTime beginDate, LocalDateTime endDate) {
        Map map = new HashMap();
        double orderCompletionRate = 0;
        double unitPrice = 0;
        map.put("beginTime", beginDate);
        map.put("endTime", endDate);

//        查询新增用户数
        Integer newUsers = loginMapper.userStatistics(map);
//        总订单
        Integer total = orderMapper.countByMap(map);

        map.put("status", Orders.COMPLETED);
//        有效订单
        Integer validOrderCount = orderMapper.countByMap(map);

//        订单完成率
        if (total != 0) {
            orderCompletionRate = (double) validOrderCount / total;
        }

//        营业额
        Double turnover = orderMapper.sumNumber(map);
        turnover = turnover == null ? 0 : turnover;

//        平均客单价
        if (orderCompletionRate != 0) {
            unitPrice = turnover / validOrderCount;
        }


        return BusinessDataVO.builder().newUsers(newUsers).orderCompletionRate(orderCompletionRate).validOrderCount(validOrderCount).turnover(turnover).unitPrice(unitPrice).build();
    }

    @Override
    public OrderOverViewVO overviewOrders() {
        Map map = new HashMap();
        map.put("beginTime", LocalDateTime.of(LocalDate.now(), LocalTime.MIN));
        map.put("endTime", LocalDateTime.of(LocalDate.now(), LocalTime.MAX));
        Integer All_Orders = orderMapper.countByMap(map);
        map.put("status", Orders.CANCELLED);
        Integer Cancelled_Orders = orderMapper.countByMap(map);
        map.put("status", Orders.COMPLETED);
        Integer Completed_Orders = orderMapper.countByMap(map);
        map.put("status", Orders.CONFIRMED);
        Integer Delivered_Orders = orderMapper.countByMap(map);
        map.put("status", Orders.TO_BE_CONFIRMED);
        Integer Pending_Orders = orderMapper.countByMap(map);
        return OrderOverViewVO.builder().allOrders(All_Orders).cancelledOrders(Cancelled_Orders).completedOrders(Completed_Orders).deliveredOrders(Delivered_Orders).waitingOrders(Pending_Orders).build();
    }

    @Override
    public DishOverViewVO overviewDishes() {
        Map map = new HashMap();

        map.put("status", StatusConstant.ENABLE);
        Integer sold = dishMapper.countByMap(map);

        map.put("status", StatusConstant.DISABLE);
        Integer discontinued = dishMapper.countByMap(map);
        return new DishOverViewVO(sold, discontinued);
    }

    @Override
    public SetmealOverViewVO overviewSetmeals() {
        Map map = new HashMap();

        map.put("status", StatusConstant.ENABLE);
        Integer sold = setmealMapper.countByMap(map);

        map.put("status", StatusConstant.DISABLE);
        Integer discontinued = setmealMapper.countByMap(map);
        return new SetmealOverViewVO(sold, discontinued);
    }
}
